<?php
/**
 *  linkfield.php 异步获取数据
 *
 * @contact			    qq:27025011
 * @license				http://www.phpcms.cn/license/
 * @lastmodify			2011-6-15 17:03
 *api.php?op=linkfieldajax&act=linkfield_ajax_search&value=nn&table_name=v9_category&select_title=catid,catname&like_title=catname&set_title=catname
 */
defined('IN_PHPCMS') or exit('No permission resources.');

switch($_GET['act']) 
{
	case 'linkfield_ajax_search':
		linkfield_ajax_search();
	break;
}
function linkfield_ajax_search() 
{
	$db = pc_base::load_model('get_model');
	$siteid = get_siteid();
	$data = array();
	//print_r($_GET);
	if($_GET['value']) 
	{
		$value = trim($_GET['value']);
		if (CHARSET == 'gbk') $value = iconv('utf-8','gbk',$value);
		$table_name = trim($_GET['table_name']);
		$db->query("SHOW COLUMNS FROM $table_name");
		while($r = $db->fetch_next()) {
			if($r['Key'] == 'PRI') break;
		}
		$order_id = $r['Field'];
		$select_title = trim($_GET['select_title']);
		$like_title = trim($_GET['like_title']);
		$set_title = trim($_GET['set_title']);
		$limit = 20;
		$sql = "SELECT $select_title from $table_name where $like_title LIKE('%$value%') order by $order_id desc limit 0,$limit";
		//echo ($sql);
		$result = $db->query($sql);
		while(($s = $db->fetch_next()) != false) 
		{
			$data[] = $s;
		}
		if (CHARSET == 'gbk') 
		{
			$data = array_iconv($data, 'gbk', 'utf-8');
		}
		echo json_encode($data);
	}
}
?>